<?php

class Admin_model extends CI_Model {
	
	public $insideAdminModel;
	
	function Admin_model() {
		parent::__construct();
		
		
	}
	
	
	
	/**
	 * Web site layout information
	 * @return $data - array with layoutinfo
	 */
	public function getLayoutInfo() {
		
		// path location of the web site
		$data['base_url']				= $this->config->item('base_url');
		
		// create the links to translate the page
		$switch_lang				= 	anchor($this->lang->switch_uri('en'),'en')." | ";
		$switch_lang				.= 	anchor($this->lang->switch_uri('pt'),'pt');
		$data['switch_lang']		= 	$switch_lang;
		
		// load the content text data ( all data is loaded ... it doesnt look the perfect solution ... find another one :0) )
		$data['text_main']			= $this->lang->line('admin.text.main');
		$data['text_insert']		= $this->lang->line('admin.text.insert');
		$data['text_list']			= $this->lang->line('admin.text.list');
		$data['text_profile']		= $this->lang->line('admin.text.profile');
		
		// ... more data
		$data['website_title']		= 'Radical Booking';
		$data['title']		 		= 'Admin';
		$data['login']				= anchor("Admin/logout","logout")." ".$this->ion_auth->get_user()->email;
		$data['subtitle'] 			= $this->lang->line('admin.text.subtitle');
		$data['footer'] 			= '� copyright Softingal';
		
		return $data;
	} 
	
	
	
	
	
	
	
	
	/**
	 * Add a user with the library Ion Auth
	 * @param Array $arr - An array with this info: user, pass, mail, meta(an array with meta data), group
	 * @return JSON object with the success(... return also the added user fields)/error message
	 */
	public function addUser($arr) {
		
		$user 	= $arr['username'];
		$pass	= $arr['password'];
		$mail	= $arr['email'];
		$meta	= array($arr['first_name'],$arr['last_name']);
		$group	= $arr['groupname'];
		
		// check user ...
		if ($this->ion_auth->username_check($user)) {
			return '{ "Result":"ERROR", "Message":"User already exists" }'; }
		
		// check email ...
		if ($this->ion_auth->email_check($mail)) {
			return '{ "Result":"ERROR", "Message":"eMail already exists" }';
		}
		
		// ... reach this point? create the user ...
		if($this->ion_auth->register($user,$pass,$mail,$meta,$group)) {
			
			// select all fields from the previous stored user
			$query = $this->db->query("
				select u.id, u.active,g.name as groupname,u.username,u.email,m.first_name, m.last_name 
					from 
						users   u,
						groups  g,
						meta    m
					where 
						(
					    	u.group_id 		= g.id
					    	and u.id 		= m.user_id
					    	and u.username 	= '$user'
					    )
			");
			
			// array with data from the previous selection
			$user_arr = $query->result();

			// format the data to jtable json format
			$final_arr = array("Result"=>"OK","Record"=>$user_arr);
			$user_json = json_encode($final_arr);
			
			// return json obj
			return $user_json;
		}
		else {
			return '{ "Result":"ERROR", "Message":"it was not possible to create the user" }';
		}
	}
	
	
	
	
	
	
	
	
	/**
	 * Query database for all users in the system except admin users 
	 * @return JSON file with user info
	 */
	public function getUsers_JSON() {
		
		// select all Users from school and hotel group (groups with id's 4 and 5) 
		$query = $this->db->query("
			select u.id, u.active,g.name as groupname,u.username,u.email,m.first_name, m.last_name, m.company, m.phone from 
				users   u,
				groups  g,
				meta    m
			where 
				(
					(g.id = 4 or g.id = 3) 	and 
					u.group_id 	= g.id 		and
					u.id 		= m.user_id
				)
		");
		
		// array with user data
		$user_arr = $query->result();
		
		// format the data to jtable json format
		$final_arr = array("Result"=>"OK","Records"=>$user_arr,"TotalRecordCount"=>$query->num_rows());
		$user_json = json_encode($final_arr);
		
		return $user_json;
	}
	
	
	
	public function updateUser($userID, $data) {

		if (true) {
			return '{ "Result":"OK"}';
		}
		else {
			return '{ "Result":"ERROR", 	"Message":"error trying to update user" }';
		}
		
	}
	
	
	public function getUsers($options) {
		$this->db->select('users.id as userID, users.username as userName, groups.id as groupID, groups.name as groupName');
		$this->db->from('users');
		$this->db->join('groups','users.group_id = groups.id');
		$query = $this->db->get();
		$arrayData = $query->result();
		$this->firephp->log($arrayData);
		$jsonData = json_encode($arrayData);
		return $jsonData;
	}
	
	public function addUsers($options) {
		
	}
	
	public function updateUsers($options) {
		
	}
	
	public function deleteUsers($options) {
		
	}
	
	
	public function tmp() {
		
	}
	
	
	public function tmpDB() {
		
		
		//$this->db->update
		
		
	}
	
	
	
}